﻿Public Class ReportUT502
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		If Not Page.IsPostBack Then
			BL.Report.SetMonthList(Me.ddlMonth)
			BL.Report.SetYearList(Me.ddlYear)
		End If
    End Sub

	Protected Sub btnPDF_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnPDF.Click
		If String.IsNullOrEmpty(Me.ucPopupOfficer1.OfficerAppId) Then
			Me.Master.Alert("กรุณาระบุนักธุรกิจทีท่านต้องการออกรายงาน")
			Exit Sub
		End If

		Dim lstParam As New List(Of String)
		Dim dteEffectDate As New DateTime(Me.ddlYear.SelectedValue, Me.ddlMonth.SelectedValue, 1)
		Dim dtSource As DataTable = BL.Report.ReportUT502(Me.ddlMonth.SelectedValue, Me.ddlYear.SelectedValue, Me.ucPopupOfficer1.OfficerAppId)
        Dim dtLRPoint As DataTable = BL.Report.ReportUT502LRPoint(Me.ddlMonth.SelectedValue, Me.ddlYear.SelectedValue, Me.ucPopupOfficer1.OfficerId)
        Dim dtPointUsed As DataTable = BL.Report.ReportUT502PointUsed(Me.ddlMonth.SelectedValue, Me.ddlYear.SelectedValue, Me.ucPopupOfficer1.OfficerId)

		If dtSource IsNot Nothing AndAlso dtSource.Rows.Count > 0 Then
            lstParam.Add(BL.SystemConfig.GetRVBalance(dtSource.Rows(0)("CAL_DATE"), dtSource.Rows(0)("INCOME_MYSELF")))
		Else
			lstParam.Add("1")
        End If

        If dtLRPoint IsNot Nothing AndAlso dtLRPoint.Rows.Count > 0 Then
            Dim LRPoint As Decimal = 0
            LRPoint = dtLRPoint.Rows(0)("LEFT_POINT_SUMMARY") + dtLRPoint.Rows(0)("RIGHT_POINT_SUMMARY")
            If dtPointUsed IsNot Nothing AndAlso dtPointUsed.Rows.Count > 0 Then
                LRPoint -= dtPointUsed.Rows(0)("POINT_USED_TOTAL")
            End If
            lstParam.Add(LRPoint)
        Else
            lstParam.Add("0")
        End If

		Me.Session("reportparameter") = lstParam
		Me.Session("reportdatasource") = dtSource

		Me.ClientScript.RegisterClientScriptBlock(Me.GetType, "ut502", "window.open('report/exportpdf.aspx?rptname=ut502', 'exportpdf','')", True)
	End Sub
End Class